package com.joyd.joydlib.dev

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment

/**
 * 屏幕点击示例Activity
 */
class ScreenTapActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_screen_tap)
        
        // 设置按钮点击事件
        findViewById<Button>(R.id.btn_tap_center).setOnClickListener {
            tapCenterScreen()
        }
        
        findViewById<Button>(R.id.btn_tap_custom).setOnClickListener {
            val xEditText = findViewById<EditText>(R.id.et_x_coordinate)
            val yEditText = findViewById<EditText>(R.id.et_y_coordinate)
            
            try {
                val x = xEditText.text.toString().toFloat()
                val y = yEditText.text.toString().toFloat()
                tapCustomPosition(x, y)
            } catch (e: NumberFormatException) {
                Toast.makeText(this, "请输入有效的坐标值", Toast.LENGTH_SHORT).show()
            }
        }
        
        findViewById<Button>(R.id.btn_tap_relative).setOnClickListener {
            val relativeXEditText = findViewById<EditText>(R.id.et_relative_x)
            val relativeYEditText = findViewById<EditText>(R.id.et_relative_y)
            
            try {
                val relativeX = relativeXEditText.text.toString().toFloat()
                val relativeY = relativeYEditText.text.toString().toFloat()
                
                if (relativeX < 0 || relativeX > 1 || relativeY < 0 || relativeY > 1) {
                    Toast.makeText(this, "请输入有效的相对坐标值(0.0-1.0)", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
                
                tapRelativePosition(relativeX, relativeY)
            } catch (e: NumberFormatException) {
                Toast.makeText(this, "请输入有效的相对坐标值", Toast.LENGTH_SHORT).show()
            }
        }
    }
    
    /**
     * 点击屏幕中心点
     */
    private fun tapCenterScreen() {
        val screenTapHelper = ScreenTapHelper.getInstance(this)
        screenTapHelper.tapCenterScreen(object : ScreenTapHelper.OnTapListener {
            override fun onTapSuccess() {
                runOnUiThread {
                    Toast.makeText(this@ScreenTapActivity, "已点击屏幕中心点", Toast.LENGTH_SHORT).show()
                }
            }
            
            override fun onTapFailure(exception: Exception) {
                runOnUiThread {
                    Toast.makeText(this@ScreenTapActivity, "点击失败: ${exception.message}", Toast.LENGTH_SHORT).show()
                }
            }
        })
    }
    
    /**
     * 点击自定义坐标位置
     */
    private fun tapCustomPosition(x: Float, y: Float) {
        val screenTapHelper = ScreenTapHelper.getInstance(this)
        screenTapHelper.tapScreen(x, y, object : ScreenTapHelper.OnTapListener {
            override fun onTapSuccess() {
                runOnUiThread {
                    Toast.makeText(this@ScreenTapActivity, "已点击坐标($x, $y)", Toast.LENGTH_SHORT).show()
                }
            }
            
            override fun onTapFailure(exception: Exception) {
                runOnUiThread {
                    Toast.makeText(this@ScreenTapActivity, "点击失败: ${exception.message}", Toast.LENGTH_SHORT).show()
                }
            }
        })
    }
    
    /**
     * 点击相对位置
     */
    private fun tapRelativePosition(relativeX: Float, relativeY: Float) {
        val screenTapHelper = ScreenTapHelper.getInstance(this)
        screenTapHelper.tapRelativePosition(relativeX, relativeY, object : ScreenTapHelper.OnTapListener {
            override fun onTapSuccess() {
                runOnUiThread {
                    Toast.makeText(this@ScreenTapActivity, "已点击相对位置($relativeX, $relativeY)", Toast.LENGTH_SHORT).show()
                }
            }
            
            override fun onTapFailure(exception: Exception) {
                runOnUiThread {
                    Toast.makeText(this@ScreenTapActivity, "点击失败: ${exception.message}", Toast.LENGTH_SHORT).show()
                }
            }
        })
    }
    
    /**
     * 屏幕点击示例Fragment
     * 可以嵌入到其他Activity中使用
     */
    class ScreenTapFragment : Fragment() {
        
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            val view = inflater.inflate(R.layout.fragment_screen_tap, container, false)
            
            // 设置按钮点击事件
            view.findViewById<Button>(R.id.btn_fragment_tap_center).setOnClickListener {
                tapCenterScreen()
            }
            
            view.findViewById<Button>(R.id.btn_fragment_tap_custom).setOnClickListener {
                val xEditText = view.findViewById<EditText>(R.id.et_fragment_x_coordinate)
                val yEditText = view.findViewById<EditText>(R.id.et_fragment_y_coordinate)
                
                try {
                    val x = xEditText.text.toString().toFloat()
                    val y = yEditText.text.toString().toFloat()
                    tapCustomPosition(x, y)
                } catch (e: NumberFormatException) {
                    Toast.makeText(context, "请输入有效的坐标值", Toast.LENGTH_SHORT).show()
                }
            }
            
            return view
        }
        
        /**
         * 点击屏幕中心点
         */
        private fun tapCenterScreen() {
            context?.let { ctx ->
                val screenTapHelper = ScreenTapHelper.getInstance(ctx)
                screenTapHelper.tapCenterScreen(object : ScreenTapHelper.OnTapListener {
                    override fun onTapSuccess() {
                        activity?.runOnUiThread {
                            Toast.makeText(ctx, "已点击屏幕中心点", Toast.LENGTH_SHORT).show()
                        }
                    }

                    override fun onTapFailure(exception: Exception) {
                        activity?.runOnUiThread {
                            Toast.makeText(ctx, "点击失败: ${exception.message}", Toast.LENGTH_SHORT).show()
                        }
                    }
                })
            }
        }
        
        /**
         * 点击自定义坐标位置
         */
        private fun tapCustomPosition(x: Float, y: Float) {
            context?.let { ctx ->
                val screenTapHelper = ScreenTapHelper.getInstance(ctx)
                screenTapHelper.tapScreen(x, y, object : ScreenTapHelper.OnTapListener {
                    override fun onTapSuccess() {
                        activity?.runOnUiThread {
                            Toast.makeText(ctx, "已点击坐标($x, $y)", Toast.LENGTH_SHORT).show()
                        }
                    }

                    override fun onTapFailure(exception: Exception) {
                        activity?.runOnUiThread {
                            Toast.makeText(ctx, "点击失败: ${exception.message}", Toast.LENGTH_SHORT).show()
                        }
                    }
                })
            }
        }
    }
}